﻿using BaseData.Domain;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Depart;
using BaseData.Infrastructure.DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.ReadAPI.Controllers
{
    /// <summary>
    /// 部门管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [ApiExplorerSettings(GroupName = "Depart")]
    public class DepartController : ControllerBase
    {
        private readonly IDepartService departService;

        public DepartController(IDepartService departService)
        {
            this.departService = departService;
        }

        #region 部门管理
        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="depts"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddDepart(Depts depts)
        {
            var res = await departService.AddDepart(depts);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "添加部门成功",
                Data = res
            };
        }
        /// <summary>
        /// 显示部门下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<Depts>>> GetAllDepts()
        {
            var res = await departService.GetAllDepts();
            return new APIResult<List<Depts>>
            {
                Code = CodeEnum.Success,
                Message = "显示部门成功",
                Data = res
            };
        }
        /// <summary>
        /// 显示部门列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<PaggingResult<List<DeptDto>>>> GetDeptPage(int index, int size, string? name)
        {
            var res = await departService.GetDeptPage(index, size, name);
            return new APIResult<PaggingResult<List<DeptDto>>>
            {
                Code = CodeEnum.Success,
                Message = "显示部门成功",
                Data = res
            };
        }
        /// <summary>
        /// 修改成功
        /// </summary>
        /// <param name="depts"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<APIResult<int>> UpdDeparts(Depts depts)
        {
            var res = await departService.UpdDept(depts);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "修改部门成功",
                Data = res
            };
        }
        /// <summary>
        /// 单删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIResult<int>> DelDept(int id)
        {
            var res = await departService.DelDept(id);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "删除成功",
                Data = 1
            };
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<APIResult<int>> DelDeptAll(string ids)
        {
            await departService.DelDeptAll(ids);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "批量删除成功",
                Data = 1
            };
        }
        /// <summary>
        /// 显示部门树
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<TreeItem>>> GetDepartTree(int parentid)
        {
            var res = await departService.GetDepartTree(parentid);
            return new APIResult<List<TreeItem>>
            {
                Code = CodeEnum.Success,
                Message = "显示部门成功",
                Data = res
            };
        }
        /// <summary>
        /// 设置状态
        /// </summary>
        /// <param name="deptId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateState(int deptId, bool state)
        {
            var res = await departService.UpdateState(deptId, state);
            return new APIResult<int>
            {
                Code = CodeEnum.Success,
                Message = "设置状态成功",
                Data = res
            };
        }
        #endregion
    }
}
